//
//  MTVisitorView.swift
//  MTWeiBo
//
//  Created by user on 2017/7/23.
//  Copyright © 2017年 itcast. All rights reserved.
//

import UIKit
import SnapKit

//代理协议
protocol MTVisitorViewDelegate: NSObjectProtocol {
    func loginOperation()
}

class MTVisitorView: UIView {
    
    //  点击登录或者注册需要执行的闭包
    var loginCallBack: (()->())?
    //代理
    var delegate: MTVisitorViewDelegate?
    
    
    
    //懒加载控件
    //旋转视图
    private lazy var cycleImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //遮罩视图
    private lazy var maskIconImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //家视图
    private lazy var homeImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //消息内容
    private lazy var massageLabel: UILabel = {
       let label = UILabel()
        //设置label的属性
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = UIColor.darkGray
        label.textAlignment = .center
        label.numberOfLines = 0
        return label
    }()
    //注册按钮
    private lazy var registerButten: UIButton = {
       let butten = UIButton()
        //点击事件
        butten.addTarget(self, action: #selector(registerButtenAction), for: .touchUpInside)
        //属性
        butten.setTitle("注册", for: .normal)
        butten.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        butten.setTitleColor(UIColor.darkGray, for: .normal)
        butten.setTitleColor(UIColor.orange, for: .highlighted)
        //背景图片
        let edgeInset = UIEdgeInsetsMake(5, 5, 5, 5)
        let image = UIImage(named: "common_button_white_disable")?.resizableImage(withCapInsets: edgeInset)
        butten.setBackgroundImage(image, for: .normal)
        //取消高亮
        butten.adjustsImageWhenHighlighted = false
        return butten
    }()
    //登录按钮
    private lazy var loginButten: UIButton = {
        let butten = UIButton()
        //点击事件
        butten.addTarget(self, action: #selector(loginButtenAction), for: .touchUpInside)
        //属性
        butten.setTitle("登录", for: .normal)
        butten.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        butten.setTitleColor(UIColor.darkGray, for: .normal)
        butten.setTitleColor(UIColor.orange, for: .highlighted)
        //背景图片
        let edgeInset = UIEdgeInsetsMake(5, 5, 5, 5)
        let image = UIImage(named: "common_button_white_disable")?.resizableImage(withCapInsets: edgeInset)
        butten.setBackgroundImage(image, for: .normal)
        //取消高亮
        butten.adjustsImageWhenHighlighted = false
        return butten
    }()

    
    
    //初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //搭建
    private func setupUI() {
        self.backgroundColor = UIColor(white: 237.0 / 255, alpha: 1.0)
        //添加子控见
        addSubview(cycleImageView)
        addSubview(maskIconImageView)
        addSubview(homeImageView)
        addSubview(massageLabel)
        addSubview(registerButten)
        addSubview(loginButten)
        //约束
        cycleImageView.snp.makeConstraints { (make) in
            make.center.equalTo(self)
        }
        maskIconImageView.snp.makeConstraints { (make) in
            make.center.equalTo(self)
        }
        homeImageView.snp.makeConstraints { (make) in
            make.center.equalTo(self)
        }
        massageLabel.snp.makeConstraints { (make) in
            make.top.equalTo(cycleImageView.snp.bottom)
            make.centerX.equalTo(self)
            make.width.equalTo(224)
        }
        registerButten.snp.makeConstraints { (make) in
            make.top.equalTo(massageLabel.snp.bottom).offset(10)
            make.leading.equalTo(massageLabel)
            make.size.equalTo(CGSize(width: 80.0, height: 35.0))
        }
        loginButten.snp.makeConstraints { (make) in
            make.top.equalTo(registerButten)
            make.trailing.equalTo(massageLabel)
            make.size.equalTo(CGSize(width: 80, height: 35))
        }
    }
    
    
    //开启旋转动画
    private func startAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //时间
        animation.duration = 20
        //目的的(角度)
        animation.toValue = 2*Double.pi
        //次数
        animation.repeatCount = MAXFLOAT
        //焦点
        animation.isRemovedOnCompletion = false
        //开启
        cycleImageView.layer.add(animation, forKey: nil)
    }
    
    //修改访客信息
    func updateVisitorInfo(message: String?,imageName: String?) {
        
        if let mas = message,let image = imageName {
            //有值表示不是首页
            massageLabel.text = mas
            cycleImageView.isHidden = true
            homeImageView.image = UIImage(named: image)
        }else{
            //是首页
            startAnimation()
        }
        
    }
    
    //按钮点击方法
    @objc private func registerButtenAction() {
        //执行闭包回调
        loginCallBack?()
        //  使用代理对象调用代理方法完成回调
        delegate?.loginOperation()
        
    }
    @objc private func loginButtenAction() {
        //执行闭包回调
        loginCallBack?()
        //  使用代理对象调用代理方法完成回调
        delegate?.loginOperation()

    }

    
}
